/*
 * Pump.h
 *
 *  Created on: May 14, 2009
 *      Author: David
 */

#ifndef PUMP_H_
#define PUMP_H_

class Pump {

public:
	Pump();
	virtual ~Pump();

	/* Monitor and report temperature sensors and control the solar water heater.
	 *
	 *
	 */
	#define VERSION          "Version 1.0 04/04/2009"
	#define LED_PIN          13
	boolean heartbeat=LOW;
	boolean current_line_is_blank = false; //web interface
	boolean tempSelect=false;     //used for multi-char input queries
	byte divisor=0;

	void setup()
	  {
	  Serial.begin(19200);
	  Serial.println("");
	  Serial.println("Initializing temperature sensors...");
	  temperatureSetup();
	  pinMode(LED_PIN, OUTPUT);  //we'll use the led to output a heartbeat
	  Serial.println("Ready.");
	  Serial.println("");
	  }

	void loop()
	  {
	  readSerial();
	  updateTemperature();
	  if (divisor--==0)
	    {
	    digitalWrite(LED_PIN, heartbeat);
	    heartbeat=heartbeat==HIGH?LOW:HIGH;
	    }
	  }

	void readSerial()
	  {
	  // check if data has been sent from the computer
	  if (Serial.available()>0)
	    {
	    byte val = Serial.read();

	    if (tempSelect)
	      {
	      if (val=='1')
	        Serial.println(getTemperature(0,1));
	      else if (val=='2')
	        Serial.println(getTemperature(0,2));
	      if (val=='3')
	        Serial.println(getTemperature(1,1));
	      else if (val=='4')
	        Serial.println(getTemperature(1,2));

	      tempSelect=false;
	      }
	    else if (val=='V' || val=='v')
	      Serial.println(VERSION);
	    else if (val=='T' || val=='t')
	      tempSelect=true;
	    else if (val=='L' || val=='l')
	      {
	      listDevices(0);
	      listDevices(1);
	      }
	    else if (val=='D' || val=='d')
	      Serial.println(divisor);
	    else
	      {
	      Serial.println("");
	      Serial.print("\"");
	      Serial.print(val);
	      Serial.println("\" is not valid.");
	      }
	    }
	  }

	/*
	void readSerial()
	  {
	  if (Serial.available()>0)
	     {
	     byte c = Serial.read();
	     // if we're at the end of the line (received a newline
	     // character) and the line is blank, the http request has ended,
	     // so we can send a reply
	     if (c == '\n' && current_line_is_blank)
	       {
	       char buf[50];
	       sprintf(buf,"%s","<html>\r\n");
	       getTemperatures(buf+strlen(buf));
	       sprintf(buf+strlen(buf),"%s","</html>");
	       // send a standard http response header
	       Serial.println("HTTP/1.0 200 OK");
	       Serial.println("Content-Type: text/html");
	       Serial.print("Content-Length: ");
	       Serial.println(strlen(buf));
	       Serial.println("");

	//       printTemperatures();
	       Serial.println(buf);
	       Serial.println("");
	       Serial.println("<br>");
	       // output the value of each humidity sensor
	       printHumidity();
	       Serial.println("");
	       Serial.println("<br>");
	       }
	       else if (c == '\n')
	         {
	         // we're starting a new line
	         current_line_is_blank = true;
	         }
	       else if (c != '\r')
	         {
	         // we've a character on the current line
	         current_line_is_blank = false;
	        }
	      }
	    }
	*/


	/*
	// this function will return the number of bytes currently free in RAM
	extern int  __bss_end;
	extern int  *__brkval;
	int freemem()
	  {
	  int free_memory;
	  if((int)__brkval == 0)
	    free_memory = ((int)&free_memory) - ((int)&__bss_end);
	  else
	    free_memory = ((int)&free_memory) - ((int)__brkval);
	  return free_memory;
	  }
	*/


};

#endif /* PUMP_H_ */
